import 'package:boss/models/taste.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/page/tasteSetting/page/plusTaste/plus_taste_page.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/page/tasteSetting/provider/taste_setting_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/button/outline_radius_button.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class TasteSettingBrowser extends StatelessWidget {
  toPlusTaste(BuildContext context, {Taste taste}) async {
    final result = await Navigator.pushNamed(context, PlusTastePage.pageName, arguments: {
      'taste': taste == null ? null : taste,
    });
    if (result != null) {
      var tasteSettingProvider = Provider.of<TasteSettingProvider>(context, listen: false);
      tasteSettingProvider.addTaste(result);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<TasteSettingProvider>(
      builder: (context, tasteSettingProvider, child) {
        Widget child = tasteSettingProvider.tastes.length == 0
            ? Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  EmptyWidget(),
                  Container(
                    margin: EdgeInsets.only(top: 50.w),
                    child: OutlineRadiusButton(
                      title: '添加口味',
                      onTap: () => toPlusTaste(context),
                    ),
                  ),
                ],
              )
            : SingleChildScrollView(
                padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 20.w),
                child: Column(
                  children: <Widget>[
                    ListView.builder(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        var current = tasteSettingProvider.tastes[index];

                        return CustomListTitle(
                          title: current.tasteName,
                          trailing: GestureDetector(
                            onTap: () {
                              showAlert(context, content: '确认要删除该口味么', onConfirm: (context) {
                                tasteSettingProvider.removeTaste(current);
                                Navigator.pop(context);
                              }, onCancel: (context) {
                                Navigator.pop(context);
                              });
                            },
                            child: BaseImage(
                              'assets/image/delete_icon.png',
                              width: 44.w,
                              height: 44.w,
                            ),
                          ),
                          subTitle: current.tasteLabel.replaceAll('|', ' | '),
                          onTap: () {
                            toPlusTaste(context, taste: current);
                          },
                        );
                      },
                      itemCount: tasteSettingProvider.tastes.length,
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 30.w),
                      child: LabelButton(
                        title: '+添加口味',
                        onTap: () => toPlusTaste(context),
                      ),
                    ),
                  ],
                ),
              );

        return Column(
          children: <Widget>[
            Expanded(child: child),
            FixedBottomBtn(
              title: '保 存',
              onTap: () {
                Navigator.pop(context, tasteSettingProvider.tastes);
              },
            ),
          ],
        );
      },
    );
  }
}
